import threadpool
import threading
from prwlock import RWLock

# create a mutex lock
lock = threading.Lock()

# create a read/write lock
rlock = RWLock()

results = []


def work_callback(reauest, result):
    results.append(result)


rlock.reader_lock()
rlock.acquire_read()
pool = threadpool.ThreadPool(10, q_size=10)


def work_func(i):
    print("Thread started")

    # try:
    #     rlock.acquire_write()
    #     print("Thread ID: %s with value: %d" % (str(threading.current_thread().ident), i))
    #     return "Thread-%d" % i
    # finally:
    #     rlock.release()

    with rlock.reader_lock():
        print("Thread ID: %s with value: %d" % (str(threading.current_thread().ident), i))
        return "Thread-%d" % i


# put requests into thread pool to process.
for i in range(4):
    workRequest = threadpool.WorkRequest(work_func,
                                         args=(i,),
                                         callback=work_callback)
    print("Put the %dth work request" % i)
    pool.putRequest(request=workRequest)

print("To execute all threads")
rlock.release()
pool.wait()
